package com.ruoyi.common.dict;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program:
 * @description:
 * @author: hyq
 * @create: 2022-01-13 19:39
 */
@Aspect
@Component
@Slf4j
public class TranslateFieldAspect {

    /**
     * 翻译字典值
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.ruoyi.common.dict.TranslationDict)")
    public Object aroundMethodDict(ProceedingJoinPoint joinPoint) throws Throwable {
        //接收到请求时间
        Long startTime = System.currentTimeMillis();
        //注意，如果调用joinPoint.proceed()方法，则修改的参数值不会生效，必须调用joinPoint.proceed(Object[] args)
        Object result = joinPoint.proceed();

        // 第一步、获取返回值类型
        Class returnType = ((MethodSignature) joinPoint.getSignature()).getReturnType();

        //首先，取出要翻译字段的字典值
        String returnJsonResult = JSONObject.toJSONString(result);
        //开始解析（翻译字段注解参数指定的字段）
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        //获取注解上参数
        TranslationDict annotation = method.getAnnotation(TranslationDict.class);
        FieldParam[] fieldParams = annotation.value();
        //遍历
        for (FieldParam fieldParam : fieldParams) {
            log.info("开始翻译字典CODE:{},取值字段：{},取值字段值类型：{}.",
                    fieldParam.dictType(),fieldParam.targetField(),fieldParam.targetFieldValueClazz());
            Pattern dictPattern = getPattern(fieldParam);
            Matcher dictMatcher=dictPattern.matcher(returnJsonResult);
            StringBuffer sb = new StringBuffer();
            //转义字段
            this.translateDict(fieldParam,dictPattern,dictMatcher,sb);
            dictMatcher.appendTail(sb);
            returnJsonResult = sb.toString();
        }

        result = JSONObject.parseObject(returnJsonResult,returnType);
        //如果这里不返回result，则目标对象实际返回值会被置为null
        //处理完请求时间
        Long endTime = System.currentTimeMillis();
        log.info("The request takes {}ms",endTime-startTime);
        return result;
    }
     /**
     * 字典值转义为中文
     * @param fieldParam
     * @param fieldPattern
     * @param fieldMatcher
     * @param sb
     */
    private void translateDict(FieldParam fieldParam, Pattern fieldPattern, Matcher fieldMatcher, StringBuffer sb) {
        //从缓存中一次性取值
//        Map<String, String> dictNames = DictData.getDictNames(fieldParam.dictType());

        //模拟缓存中的值
        Map<String, String> dictNames = new HashMap<>();
        dictNames.put("CUSTOMER_SEX_1","女");
        dictNames.put("CUSTOMER_SEX_2","男");
        dictNames.put("CUSTOMER_STATUS_2","是");
        dictNames.put("CUSTOMER_STATUS_1","否");
        while (fieldMatcher.find()){

            //取出要翻译字段对应的值
            Matcher dictValueMatcher = fieldPattern.matcher(fieldMatcher.group());
            dictValueMatcher.find();
            String group = dictValueMatcher.group();
            //""sex":1", ""sex":"1"",""sex":null"
            //属性无值
            if (group.split(":").length <= 1) continue;
            String dictName = "";

            //获取字典值
            String dictValue = group.split(":")[1].replace("\"", "");
            //属性值非为空 为空赋值空串
            if (StringUtils.isNotBlank(dictValue) && !dictValue.toLowerCase().equals("null")){
                //多值
                if (dictValue.split(",").length > 1){
                    for (String s : dictValue.split(",")) {
                        //fieldParam.dictType() + "_" + s 根据自己字典表设置的规则去查询
                        String ss = fieldParam.dictType();
                        System.out.println(ss);
                        dictName += dictNames.get(fieldParam.dictType() + "_" + s) + "/";
                    }
                }else {
                    dictName = dictNames.get(fieldParam.dictType() + "_" + dictValue);
                }
            }

            String s =  "\"" +  fieldParam.targetField() + "Str" + "\":\"" + dictName + "\"," + fieldMatcher.group();
            log.debug("拼接后字符串:{}",s);
            fieldMatcher.appendReplacement(sb, s);
        }
    }
     /**
     * 获取对应的正则式
     * @param fieldParam
     * @return
     */
    private Pattern getPattern(FieldParam fieldParam) {
        Pattern fieldPattern;//属性整型 字符型
        if (fieldParam.targetFieldValueClazz().equals(Integer.class) ){
            fieldPattern= Pattern.compile("\""+fieldParam.targetField() +"\":(\\d+)?");
        }else {
            fieldPattern= Pattern.compile("\""+fieldParam.targetField() +"\":\"([0-9a-zA-Z_,]+)?\"");
        }
        return fieldPattern;
    }
}
